<template>
  <!-- 数据置标 -->
  <div class="fileMark">
    <div class="btn-continer">
      <el-button type="primary" class="btn-lanse" @click="zhibiao"
        ><i class="el-icon-price-tag el-icon--left xz"></i>置标</el-button
      >
      <el-button type="primary" class="btn-lvse" @click="submit"
        ><i class="el-icon-check el-icon--left"> 提交</i></el-button
      >
      <div class="btn-right">
        <el-button
          icon="el-icon-search"
          @click="getList"
          type="primary"
        ></el-button>
      </div>
      <el-input
        v-model="searchValue"
        placeholder="按标签信息检索"
        clear
        class="el-inputClass"
      ></el-input>
    </div>
    <div class="table-continer">
      <el-table
        :data="tableData"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        :highlight-current-row="true"
        :header-cell-style="{ background: '#F0F0F6', color: '#2D3548' }"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="fileName" label="文件名称">
        </el-table-column>
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column prop="fileSizeUnit" label="文件大小">
        </el-table-column>

        <el-table-column prop="fileType" label="数据类型">
          <template slot-scope="scope">
            <span v-if="scope.row.fileType == 1">图片</span>
            <span v-if="scope.row.fileType == 2">视频</span>
            <span v-if="scope.row.fileType == 3">文档</span>
            <span v-if="scope.row.fileType == 4">音频</span>
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="创建人"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column label="标签名称">
          <template slot-scope="scope">
            <el-tag
              class="eltag"
              v-for="(item, index) in scope.row.fileLabel"
              :key="index + 'tag'"
              ><i :class="'el-icon--left xz'"></i>{{ item }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.fileStatus == 1">待提交</span>
            <span v-if="scope.row.fileStatus == 2">待审核</span>
            <span v-if="scope.row.fileStatus == 3">退回</span>
            <span v-if="scope.row.fileStatus == 4">审核通过</span>
            <span v-if="scope.row.fileStatus == null">待置标</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="Pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
      >
      </el-pagination>
    </div>
    <!-- 置标弹窗 -->
    <el-dialog title="置标" :visible.sync="dialogFormVisible">
      <div class="markup">
        <el-table
          :data="rankThree"
          border
          max-height="300px"
          :key="keys"
          :highlight-current-row="true"
          :header-cell-style="{ background: '#F0F0F6', color: '#2D3548' }"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          row-key="id"
          lazy
          :load="getChildrens"
          style="width: 100%"
          @selection-change="selectionClick"
          ref="multipleTable"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column fixed label="名称">
            <template slot-scope="scope">
              <span style="height: 16px; display: inline-block"
                ><img
                  src="../assets/bao.png"
                  alt=""
                  style="margin-top: 3px; display: block"
              /></span>
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="nature" label="标签性质"> </el-table-column> -->
          <!-- <el-table-column prop="valueType" label="值类型"> </el-table-column>
          <el-table-column prop="ranges" label="值域"> </el-table-column>
          <el-table-column prop="proofRule" label="验证规则"> </el-table-column> -->
          <!-- <el-table-column label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0">暂存</span>
              <span v-if="scope.row.state == 1">待审核</span>
              <span v-if="scope.row.state == 2">审核通过</span>
              <span v-if="scope.row.state == 3">否决</span>
              <span v-if="scope.row.state == 4">删除待审核</span>
            </template>
          </el-table-column> -->
          <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              ><i class="el-icon-edit el-icon--left"> 编辑</i></el-button
            >
            <el-button type="text" size="small"
              ><i class="el-icon-view el-icon--left"> 预览</i></el-button
            >
          </template>
        </el-table-column> -->
        </el-table>
        <div class="Pagination">
          <el-pagination
            @size-change="handleSizeChanges"
            @current-change="handleCurrentChanges"
            background
            :current-page="pageNum"
            :page-sizes="[10, 20, 30]"
            :page-size="pageSizes"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totals"
          >
          </el-pagination>
        </div>
        <!-- <p v-if="checkTag.length">已添加的标签({{ checkTag.length }}/5)</p>
        <div class="tag" v-if="checkTag.length">
          <el-tag
            v-for="(item, index) in checkTag"
            @close="handleClose(item.text)"
            :key="index"
            closable
            :class="item.color"
          >
            <img class="icon" :src="item.icon" alt="" />
            <span>{{ item.text }}</span>
          </el-tag>
        </div>
        <p>可添加的标签(5/5)</p>
        <div class="classify flex-center-start">
          <div class="label">一级分类:</div>
          <div class="tab-list flex-wrap">
            <span
              :class="{ chosed: choseValue1 === item.id }"
              v-for="(item, index) in rankOne"
              :key="index"
              @click="choseRank('1', item.id)"
              >{{ item.text }}</span
            >
          </div>
        </div>
        <div class="classify classify2 flex-center-start">
          <div class="label">二级分类:</div>
          <div class="tab-list tab-list2 flex-wrap">
            <span
              :class="{ chosed: choseValue2 === item.id }"
              v-for="(item, index) in rankTwo"
              :key="index"
              @click="choseRank('2', item.id)"
              >{{ item.text }}</span
            >
          </div>
        </div>
        <div class="classify classify3 flex-center-start">
          <div class="label">标签名称:</div>
          <div class="tab-detail">
            <el-checkbox-group v-model="checkList">
              <el-checkbox
                :label="item.text"
                v-for="(item, index) in rankThree"
                :key="index"
                >{{ item.text }}</el-checkbox
              >
            </el-checkbox-group>
          </div>
        </div> -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="zhibiaosumit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "fileMark",
  data() {
    return {
      currentPage: 1,
      searchValue: "",
      dialogFormVisible: false,
      choseValue1: "",
      choseValue2: "",
      checkList: [],
      checkTag: [],
      rankOne: [],
      rankTwo: [],
      rankThree: [],
      tableData: [],
      loading: false,
      totalSize: 0,
      pageSize: 10,
      selectId: [],
      firstSort: "",
      secondSort: "",
      pageSizes: 20,
      keys: 0,
      tableAllData: [],
      totals: 0,
      pageNum: 1,
    };
  },
  watch: {
    checkList(newVal, oldVal) {
      // 选择标签时,重新组成新的数组展示；
      // this.checkTag = newVal.map((item, index) => {
      //   return {
      //     text: item,
      //     color: `color${index}`,
      //     icon: require(`@/assets/icon/zhibiao0${index + 1}.png`),
      //   };
      // });
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    getChildrens(tree, treeNode, resolve) {
      let obj = {
        parentId: tree.id,
        pageNum: this.pageNum,
        pageSize: this.pageSizes,
      };
      this.$api.getList(obj).then((res) => {
        let data;
        if (res.data.list.length && res.data.list.length > 0) {
          data = res.data.list.map((item, index) => {
            return {
              ...item,
              hasChildren: true,
              children: [],
            };
          });
          this.tableAllData = this.tableAllData.concat(data); // 保存表格数据
          resolve(data);
        } else {
          tree.hasChildren = false;
          this.$set(this.rankThree, tree.id.toString() + "assd", "");
        }
      });
    },
    selectionClick(row) {
      this.checkTag = row;
      console.log(this.checkTag);
    },
    zhibiao() {
      if (!this.selectId.length) {
        this.$message({
          showClose: true,
          message: "请选择一条数据进行置标",
          type: "warning",
        });
        return;
      }
      this.dialogFormVisible = true;
      this.firstSort = "";
      this.secondSort = "";
      this.choseValue1 = "";
      this.choseValue2 = "";
      this.rankOne = [];
      this.rankTwo = [];
      this.rankThree = [];
      this.checkList = [];
      this.checkTag = [];
      this.pageNum = 1;
      this.getTagList();
    },
    zhibiaosumit() {
      console.log(this.checkTag);
      if (this.checkTag.length == 0) {
        this.$message({
          showClose: true,
          message: "请选择标签",
          type: "warning",
        });
        return;
      }

      let lable = "";
      this.checkTag.forEach((res, index) => {
        if (index == this.checkTag.length - 1) {
          lable += res.name;
        } else {
          lable += res.name + "-";
        }
      });
      let idArr = this.selectId.map((item) => {
        return item.id;
      });
      let obj = {
        id: idArr,
        lables: lable.replace(/\s+/g, ""),
      };
      this.$api
        .setFileLable(obj)
        .then((res) => {
          if (res.code == 200) {
            this.$message.closeAll();
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getList();
          }
        })
        .catch((res) => {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        });
    },
    submit() {
      // let arr = [];
      // this.selectId.forEach((res) => {
      //   arr.push({
      //     id: res.id,
      //     lables: res.fileLabel,
      //     user: window.sessionStorage["userName"],
      //   });
      // });
      if (!this.selectId.length) {
        this.$message({
          showClose: true,
          message: "请选择一条数据进行提交",
          type: "warning",
        });
        return;
      }
      let idArr = this.selectId.map((item) => {
        return item.id;
      });
      let obj = {
        id: idArr,
      };
      this.$api
        .labelSubmit(obj)
        .then((res) => {
          if (res.code == 200) {
            this.$message.closeAll();
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            this.currentPage = 1;
            this.getList();
          }
        })
        .catch((res) => {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        });
    },
    // selectionchange(row) {
    //   this.selectId = row;
    // },
    handleSelectionChange(val) {
      this.selectId = val;
    },
    getList() {
      this.loading = true;
      let obj = {
        lables: this.searchValue,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      };
      this.$api
        .fileLableList(obj)
        .then((res) => {
          // console.log(res);
          res.data.list.forEach((s) => {
            if (s.fileLabel) {
              s.fileLabel = s.fileLabel.split(";");
            }
          });
          this.tableData = res.data.list;
          this.totalSize = res.data.total;

          console.log(this.tableData);
          this.loading = false;
          this.selectId = [];
        })
        .catch(() => {
          this.loading = false;
          this.selectId = [];
        });
    },
    getTagList() {
      // this.$api
      //   .getLabel({
      //     firstSort: this.firstSort,
      //     secondSort: this.secondSort,
      //   })
      //   .then((res) => {
      //     this.rankOne = res.data.firstSort;
      //     this.rankTwo = res.data.secondSort;
      //     this.rankThree = res.data.label;
      //     console.log(this.choseValue1, this.choseValue2);
      //   });
      let obj = {
        parentId: 0,
        pageNum: this.pageNum,
        pageSize: this.pageSizes,
        state: "",
      };
      this.$api.selectListTwo2(obj).then((res) => {
        this.rankThree = res.data.list;
        this.rankThree.forEach((res) => {
          res.hasChildren = true;
        });
        this.totals = res.data.total;
        this.keys = Math.random();
      });
    },
    choseRank(type, id) {
      if (type == "1") {
        this.choseValue1 = id;
        this.firstSort = id;
        this.getTagList();
      }
      if (type == "2") {
        this.choseValue2 = id;
        this.secondSort = id;
        this.getTagList();
      }
    },
    handleClose(text) {
      this.checkTag.splice(this.checkTag.indexOf(text), 1);
      this.checkList.splice(this.checkList.indexOf(text), 1);
    },
    // handleClick(row) {
    //   this.selectId = row;
    //   // console.log(row);
    // },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
      // console.log(`当前页: ${val}`);
    },
    handleSizeChanges(val) {
      this.pageSizes = val;
      this.getTagList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChanges(val) {
      this.pageNum = val;
      this.getTagList();
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>
<style>
.el-tag .el-tag__close {
  color: #132232;
}
</style>
<style scoped lang="scss">
@import "@/css/mixin.scss";
@import "@/css/flex.scss";
.fileMark {
  padding: 0 30px;
  overflow: hidden;
  .btn-continer {
    margin-top: 30px;
    margin-bottom: 20px;
    .btn-right {
      float: right;
    }
    .xz {
      transform: rotate(-45deg);
    }
    .el-inputClass {
      float: right;
      width: 317px;
      margin-right: 10px;
    }
  }
  .Pagination {
    text-align: right;
    margin-top: 40px;
  }
  .markup {
    // width:95%;
    padding: 38px;
    border-top: 1px solid #d8d8d8;
    border-bottom: 1px solid #d8d8d8;
    p {
      @include sc(19px, #3386fe);
    }
    .tag {
      margin: 20px 0 50px 0;
    }
    .el-tag {
      margin-right: 20px;
      font-size: 18px;
      line-height: 25px;
      img {
        @include wh(20px, 20px);
        vertical-align: middle;
        display: inline-block;
      }
      span {
        vertical-align: middle;
        display: inline-block;
        margin-left: 5px;
      }
    }
    .color0 {
      color: #2285e2;
      background-color: #eff5ff;
      border-color: #eff5ff;
    }
    .color1 {
      color: #00d815;
      background-color: #ebfcec;
      border-color: #ebfcec;
    }
    .color2 {
      color: #ff8f2c;
      background-color: #fff6ee;
      border-color: #fff6ee;
    }
    .color3 {
      color: #2285e2;
      background-color: #eff5ff;
      border-color: #eff5ff;
    }
    .color4 {
      color: #fc4242;
      background-color: #fff0f0;
      border-color: #fff0f0;
    }
    .color5 {
      color: #fc4242;
      background-color: #fff0f0;
      border-color: #fff0f0;
    }
    .color6 {
      color: #fc4242;
      background-color: #fff0f0;
      border-color: #fff0f0;
    }
    .color7 {
      color: #fc4242;
      background-color: #fff0f0;
      border-color: #fff0f0;
    }
    .classify {
      margin-top: 30px;
      align-items: baseline;
      .label {
        @include sc(18px, #515c68);
        font-weight: bold;
        margin-right: 7px;
      }

      .tab-list {
        width: calc(100% - 100px);
        cursor: pointer;
        span {
          padding: 10px 20px;
          background: #f0f0f6;
          border-radius: 5px;
          margin: 0 10px 15px 0;
        }
        .chosed {
          color: #3386fe;
          background-color: #eff5ff;
        }
      }
      .tab-detail {
        width: calc(100% - 100px);
        min-height: 130px;
        background-color: #f3f3f7;
        padding: 18px 22px;
        border-radius: 5px;
        overflow-y: scroll;
        /deep/.el-checkbox__inner {
          border-radius: 50%;
          background-color: #a8a8a8;
        }
        /deep/.el-checkbox__input.is-checked + .el-checkbox__label {
          // color:#6c7c92;
        }
        /deep/.el-checkbox__input.is-checked .el-checkbox__inner {
          // background-color: #6c7c92;
          // border-color:#6c7c92;
          background-color: #409eff;
        }
      }
    }
    .classify2 {
      margin-top: 10px;
      .tab-list {
        span {
        }
      }
    }
    .classify3 {
      align-items: flex-start;
    }
  }
  .eltag {
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
